// pages/renovationBill/index.js
//引入请求文件
var util = require('../../utils/util.js');
let app = getApp();
let url = app.url;
let gobalData = app.gobalData;
Page({
  /**
   * 页面的初始数据
   */
  data: {
		sumPrice: 0,  //全部支出
		count: 0,    // 消费笔数
		monthSumPrice: 0,  //本月花销
		list: [],
		startX: 0, //开始坐标
    startY: 0,
		hasFlag: false,
		shareId: null, //分享者主键
		isBind: 0,   //判断账单是否共享
		leaderId: null //预留邀请好友注册接口
  },
// 点击执行弹出模态框---找客服
	SupervisorConsultation: function(){
    wx.showModal({
      title: '咨询电话',
      content: '400-1000-466',
      cancelColor: '#fd8439',
      confirmText: '拨打电话',
      confirmColor: '#fd8439',
      success: function (res) {
        // 判断用户点击的是拨打电话还是取消
        if (res.confirm) {
          wx.makePhoneCall({
            phoneNumber: '4001000466'
          })
        }
      }
    })
  },
	// 用户共享成功
	shareSuccess(res){
		this.onShow();
		wx.showToast({
			title: '加入成功！'
		})
	},
	gobillInfo(e){
		let id = e.currentTarget.dataset.id; 
		// 跳转到对应的账单详情里面
		wx.navigateTo({
			url: "/pages/OnePenInfo/index?id=" + id
		})
	},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
		console.log(options);
		this.setData({
			shareId: options.shareId,
			isBind: options.isBind,
			leaderId: options.leaderId
		})
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
	delect(e){
		util(`${url}Billing/delBilling`,{
			id: e.currentTarget.dataset.id
		}).then(res=>{
			if (res.status == 1){
				let cid = wx.getStorageSync('userInfo').userInfo.id;
				return util(`${url}Billing/getBillingDetail`, {cid});
			}
		}).then(res=>{
			this.setData({
				sumPrice: res.content.sumPrice,  //全部支出
				count: res.content.count,    // 消费笔数
				monthSumPrice: res.content.monthSumPrice,  //本月花销
				list: res.content.List
			})
			if ( res.content.List.length == 0 ) {
				this.setData({
					hasFlag: true
				})
			} else {
				this.setData({
					hasFlag: false
				})
			}
		})
	},
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
		let cid = wx.getStorageSync('userInfo').userInfo.id;
		util(`${url}Billing/getBillingDetail`, {cid}).then(res=>{
			if (res.status == 1) {
				this.setData({
					sumPrice: res.content.sumPrice,  //全部支出
					count: res.content.count,    // 消费笔数
					monthSumPrice: res.content.monthSumPrice,  //本月花销
					list: res.content.List
				})
				if ( res.content.List.length == 0 ) {
					this.setData({
						hasFlag: true
					})
				} else {
					this.setData({
						hasFlag: false
					})
				}
			} else {
        if (this.data.list.length == 0) {
          this.setData({
            hasFlag: true
          })
        } else {
          this.setData({
            hasFlag: false
          })
        }
				throw res.msg;
			}
		})
  },
//手指触摸动作开始 记录起点X坐标
	touchstart: function(e) {
		//开始触摸时 重置所有删除
		this.data.list.forEach(function(v, i) {
			if (v.isTouchMove) //只操作为true的
				v.isTouchMove = false;
		})
		this.setData({
			startX: e.changedTouches[0].clientX,
			startY: e.changedTouches[0].clientY,
			list: this.data.list
		})
	},
	//滑动事件处理
	touchmove: function(e) {
		var that = this,
			index = e.currentTarget.dataset.index, //当前索引
			startX = that.data.startX, //开始X坐标
			startY = that.data.startY, //开始Y坐标
			touchMoveX = e.changedTouches[0].clientX, //滑动变化坐标
			touchMoveY = e.changedTouches[0].clientY, //滑动变化坐标
			//获取滑动角度
			angle = that.angle({
				X: startX,
				Y: startY
			}, {
				X: touchMoveX,
				Y: touchMoveY
			});

		that.data.list.forEach(function(v, i) {
			v.isTouchMove = false
			//滑动超过30度角 return
			if (Math.abs(angle) > 30) return;
			if (i == index) {
				if (touchMoveX > startX) //右滑
					v.isTouchMove = false
				else //左滑
					v.isTouchMove = true
			}
		})
		//更新数据
		that.setData({
			list: that.data.list
		})
	},
	/**

	* 计算滑动角度

	* @param {Object} start 起点坐标

	* @param {Object} end 终点坐标

	*/
	angle: function(start, end) {
		var _X = end.X - start.X,
			_Y = end.Y - start.Y
		//返回角度 /Math.atan()返回数字的反正切值
		return 360 * Math.atan(_Y / _X) / (2 * Math.PI);
	}
})